package com.youwu.demo06.Thread01;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class MapExample {
    private static int threadTotal = 200;
    private static int clientTotal = 5000;

    private static Map<Integer, Integer> map = new HashMap<Integer, Integer>();

    public static void main(String[] args) {
        ExecutorService exe = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        for (int index = 0; index < clientTotal; index++) {
            final int threadNum = index;
            exe.execute(() -> {
                try {
                    semaphore.acquire();
                    func(threadNum);
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        exe.shutdown();
        System.out.println(map.size());
    }

    private static void func(int threadTotal) {
        map.put(threadTotal, threadTotal);
    }
}
